﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.IO;
using IWshRuntimeLibrary;

namespace XbdSoft.WPFVersion
{
	/// <summary>
	/// Interaction logic for ConfigurationWindow.xaml
	/// </summary>
	public partial class ConfigurationWindow : Window
	{
        private XbdConfiguration Configuration = Util.Configuration;

		public ConfigurationWindow()
		{
			this.InitializeComponent(); 
			
			// Insert code required on object creation below this point.
		}

        private void SelectFolderButton_Click(object sender, RoutedEventArgs e)
        {
            var folderBrowserDialog = new FolderBrowserDialog();
            if (folderBrowserDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            { 
                SaveFilePathTextBox.Text = folderBrowserDialog.SelectedPath;
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SaveFilePathTextBox.Text = Configuration.SaveFilePath;
            var allData = new List<int>() { 2,4,6,8};
            MaxDownloadNumComboBox.ItemsSource = allData;
            var index = allData.IndexOf(Configuration.MaxDownloadNum);
            MaxDownloadNumComboBox.SelectedIndex = index >= 0 ? index : 0;
            AutoStartCheckBox.IsChecked = Configuration.AutoStart;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (Directory.Exists(SaveFilePathTextBox.Text))
            {
                Configuration.SaveFilePath = SaveFilePathTextBox.Text;
            }
            Configuration.MaxDownloadNum = (int)MaxDownloadNumComboBox.SelectedValue;
            Configuration.AutoStart = AutoStartCheckBox.IsChecked == true; 
            XbdConfiguration.Save(Configuration);
            try
            {
                if (Configuration.AutoStart)
                {
                    CreateLink();
                }
                else {
                    DeleteLink();
                }
            }
            catch(Exception ex) {
                System.Windows.MessageBox.Show("您没有权限访问，请您以管理员帐户运行该程序.");
            }
            this.Close();
        }

        public void CreateLink()
        {
            WshShell shell = new WshShell();
            IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(LinkPath + "\\" + "小布丁软件.lnk");
            shortcut.TargetPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            shortcut.WorkingDirectory = System.Environment.CurrentDirectory;
            shortcut.WindowStyle = 1;
            shortcut.Description = "小布丁软件";
            shortcut.IconLocation = System.Environment.CurrentDirectory + "\\" + "favicon.ico";
            shortcut.Save();
        }

        public void DeleteLink()
        {
            var linkPath = LinkPath +
            "\\" + "小布丁软件.lnk";
            if (System.IO.File.Exists(linkPath))
            {
                System.IO.File.Delete(linkPath);
            }
        }

        private static string LinkPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartup);
	}
}